package ml.classification.filter;

import java.util.ArrayList;

public class SpecialisedSpeechFilter extends PostProcessingFilter {

	public SpecialisedSpeechFilter(final int n, final int iterations,
			final double threshold) {
		super();
		this.n = n;
		this.iterations = iterations;
		this.threshold = threshold;
	}

	@Override
	public ArrayList<Integer> apply(final ArrayList<Integer> list) {
		final ArrayList<Integer> newList = new ArrayList<>();
		ArrayList<Integer> currentList = list;
		int votedTrue;
		int votedFalse;

		for (int i = 0; i < iterations; i++) {
			for (int j = 0; j < currentList.size(); j++) {
				if (currentList.get(j) == 1) {
					newList.add(j, 1);
				} else {
					votedTrue = 0;
					votedFalse = 0;
					for (int k = j - n; k <= j + n; k++) {
						if (k >= 0 && k < currentList.size()) {
							if (currentList.get(k) == 1) {
								votedTrue++;
							} else {
								votedFalse++;
							}
						}
					}
					final double ratio = votedFalse
							/ ((double) (votedTrue + votedFalse));
					if (ratio >= threshold) {
						newList.add(j, 0);
					} else {
						newList.add(j, 1);
					}
				}
			}
			currentList = new ArrayList<>();
			for (final Integer x : newList) {
				currentList.add(x);
			}
		}
		return currentList;
	}

	@Override
	public String getFilterName() {
		return "SpecialisedSpeechFilter";
	}

}
